﻿Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
''' <summary>
''' 用于储存计算结果的类。包括了网格和关心点的浓度、干沉降、湿沉降的数据
''' </summary>
''' <remarks></remarks>
<Serializable()> Public Class ResultNameAndData
    Public m_Name As String

    Public m_MaxValueConDEPOS As ResultConDEPOS  '网格最大浓度值
    Public m_MinValueConDEPOS As ResultConDEPOS '网格最小浓度值
    Public m_DiscConDEPOS(-1) As ResultConDEPOS '储存关心点浓度最大的数据
    Public m_SubGridConDEPOS(-1)(,) As ResultConDEPOS '用于储存嵌套网格中的浓度数据

    ''' <summary>
    ''' 从结果数据文件中读取数据。
    ''' </summary>
    ''' <param name="Receptor">接受点对象</param>
    ''' <param name="IsDep">是否考虑干湿沉降</param>
    ''' <remarks></remarks>
    Public Function GetDataFromPlotFile(ByVal Receptor As Receptor, ByVal IsDep As Boolean, ByVal FileName As String) As Boolean
        '初始化数组
        Dim m_Data As String
        Try
            m_Data = My.Computer.FileSystem.ReadAllText(FileName)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try

        Dim nstart As Integer = 0
        Dim nend As Integer = m_Data.IndexOf(vbCrLf) '取得第一个回车符的位置
        Dim substr As String = m_Data.Substring(0, nend - nstart) '取得第一行
        While substr.IndexOf("*") >= 0
            nstart = nend + 1
            nend = m_Data.IndexOf(vbCrLf, nstart)
            substr = m_Data.Substring(nstart + 1, nend - nstart - 1)
        End While
        ReDim m_SubGridConDEPOS(Receptor.SubGridCart.Length - 1)
        For iSub As Integer = 0 To Receptor.SubGridCart.Length - 1
            If Receptor.SubGridCart(iSub).Selected = True Then
                Dim rows As Integer = Receptor.SubGridCart(iSub).Ynum
                Dim cols As Integer = Receptor.SubGridCart(iSub).Xnum
                Dim GridConDEPOS(rows - 1, cols - 1) As ResultConDEPOS
                For i As Integer = 0 To rows - 1
                    For j As Integer = 0 To cols - 1
                        GridConDEPOS(rows - 1 - i, j) = New ResultConDEPOS
                        Try
                            GridConDEPOS(rows - 1 - i, j).x = CAermod.StringHandle.GetString(substr, 1) '关心点的X坐标
                            GridConDEPOS(rows - 1 - i, j).y = CAermod.StringHandle.GetString(substr, 2) '关心点的Y坐标
                            GridConDEPOS(rows - 1 - i, j).con = CAermod.StringHandle.GetString(substr, 3) '浓度
                            If IsDep Then
                                GridConDEPOS(rows - 1 - i, j).DEPOS = CAermod.StringHandle.GetString(substr, 4)
                                GridConDEPOS(rows - 1 - i, j).DDEP = CAermod.StringHandle.GetString(substr, 5)
                                GridConDEPOS(rows - 1 - i, j).WDEP = CAermod.StringHandle.GetString(substr, 6)
                                GridConDEPOS(rows - 1 - i, j).dateD = CAermod.StringHandle.GetString(substr, 14) '关心点的日期

                                GridConDEPOS(rows - 1 - i, j).z = CAermod.StringHandle.GetString(substr, 7)

                            Else
                                GridConDEPOS(rows - 1 - i, j).z = CAermod.StringHandle.GetString(substr, 4)
                                GridConDEPOS(rows - 1 - i, j).dateD = CAermod.StringHandle.GetString(substr, 11) '关心点的日期
                            End If
                            If GridConDEPOS(rows - 1 - i, j).con > Me.m_MaxValueConDEPOS.con Then '求得最大值
                                Me.m_MaxValueConDEPOS.con = GridConDEPOS(rows - 1 - i, j).con
                                Me.m_MaxValueConDEPOS.dateD = GridConDEPOS(rows - 1 - i, j).dateD
                                Me.m_MaxValueConDEPOS.x = GridConDEPOS(rows - 1 - i, j).x
                                Me.m_MaxValueConDEPOS.y = GridConDEPOS(rows - 1 - i, j).y
                                Me.m_MaxValueConDEPOS.z = GridConDEPOS(rows - 1 - i, j).z
                                Me.m_MaxValueConDEPOS.DEPOS = GridConDEPOS(rows - 1 - i, j).DEPOS
                                Me.m_MaxValueConDEPOS.DDEP = GridConDEPOS(rows - 1 - i, j).DDEP
                                Me.m_MaxValueConDEPOS.WDEP = GridConDEPOS(rows - 1 - i, j).WDEP
                            End If
                            If GridConDEPOS(rows - 1 - i, j).con < Me.m_MinValueConDEPOS.con And GridConDEPOS(rows - 1 - i, j).con <> 0 Then '求得最小值
                                Me.m_MinValueConDEPOS.con = GridConDEPOS(rows - 1 - i, j).con
                                Me.m_MinValueConDEPOS.dateD = GridConDEPOS(rows - 1 - i, j).dateD
                                Me.m_MinValueConDEPOS.x = GridConDEPOS(rows - 1 - i, j).x
                                Me.m_MinValueConDEPOS.y = GridConDEPOS(rows - 1 - i, j).y
                                Me.m_MinValueConDEPOS.z = GridConDEPOS(rows - 1 - i, j).z
                                Me.m_MinValueConDEPOS.DEPOS = GridConDEPOS(rows - 1 - i, j).DEPOS
                                Me.m_MinValueConDEPOS.DDEP = GridConDEPOS(rows - 1 - i, j).DDEP
                                Me.m_MinValueConDEPOS.WDEP = GridConDEPOS(rows - 1 - i, j).WDEP

                            End If
                            nstart = nend + 1
                            nend = m_Data.IndexOf(vbCrLf, nstart)
                            If nend > 0 Then
                                substr = m_Data.Substring(nstart + 1, nend - nstart - 1)
                            Else
                                Exit For
                            End If
                        Catch ex As Exception
                            MsgBox("您输入的污染物源强可能太大，已经超出了aermod的范围")
                        End Try
                    Next
                Next
                m_SubGridConDEPOS(iSub) = GridConDEPOS
            End If
        Next

        ReDim m_DiscConDEPOS(Receptor.DiscCarts.Length - 1) '储存关心点浓度最大的数据
        If IsDep = True Then '考虑干湿沉降
            For i As Integer = 0 To m_DiscConDEPOS.Length - 1
                If Receptor.DiscCarts(i).Selected = True Then
                    m_DiscConDEPOS(i) = New ResultConDEPOS
                    m_DiscConDEPOS(i).x = CAermod.StringHandle.GetString(substr, 1) '关心点的X坐标
                    m_DiscConDEPOS(i).y = CAermod.StringHandle.GetString(substr, 2) '关心点的Y坐标
                    m_DiscConDEPOS(i).con = CAermod.StringHandle.GetString(substr, 3) '关心点的浓度值
                    m_DiscConDEPOS(i).DEPOS = CAermod.StringHandle.GetString(substr, 4) '关心点的总沉积
                    m_DiscConDEPOS(i).DDEP = CAermod.StringHandle.GetString(substr, 5) '关心点的干沉积
                    m_DiscConDEPOS(i).WDEP = CAermod.StringHandle.GetString(substr, 6) '关心点的湿沉积

                    m_DiscConDEPOS(i).dateD = CAermod.StringHandle.GetString(substr, 13) '关心点的日期
                    nstart = nend + 1
                    nend = m_Data.IndexOf(vbCrLf, nstart)
                    If nend > 0 Then
                        substr = m_Data.Substring(nstart + 1, nend - nstart - 1)
                    Else
                        Exit Function
                    End If
                End If
            Next
        Else '不考虑干湿沉降
            For i As Integer = 0 To m_DiscConDEPOS.Length - 1
                If Receptor.DiscCarts(i).Selected = True Then
                    m_DiscConDEPOS(i) = New ResultConDEPOS
                    m_DiscConDEPOS(i).x = CAermod.StringHandle.GetString(substr, 1) '关心点的X坐标
                    m_DiscConDEPOS(i).y = CAermod.StringHandle.GetString(substr, 2) '关心点的Y坐标
                    m_DiscConDEPOS(i).con = CAermod.StringHandle.GetString(substr, 3) '关心点的浓度值
                    m_DiscConDEPOS(i).dateD = CAermod.StringHandle.GetString(substr, 10) '关心点的日期
                    nstart = nend + 1
                    nend = m_Data.IndexOf(vbCrLf, nstart)
                    If nend > 0 Then
                        substr = m_Data.Substring(nstart + 1, nend - nstart - 1)
                    Else
                        Exit Function
                    End If
                End If
            Next
        End If
    End Function
End Class

